package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.domain.po.Teacher;
import com.campus.mapper.TeacherMapper;
import com.campus.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Teacher saveTeacher(Teacher teacher) {
        Teacher existingTeacher = getTeacherByName(teacher.getTeacherName());
        if (existingTeacher == null) {
            this.save(teacher);
            return teacher;
        } else {
            Integer id = existingTeacher.getId();
            BeanUtils.copyProperties(teacher, existingTeacher);
            existingTeacher.setId(id);
            this.updateById(existingTeacher);
            return existingTeacher;
        }
    }

    @Override
    public Teacher getTeacherById(Integer id) {
        return this.getById(id);
    }

    @Override
    public Teacher updateTeacher(Teacher teacher) {
        this.updateById(teacher);
        return this.getById(teacher.getId());
    }

    @Override
    public boolean deleteTeacherById(Integer id) {
        return this.removeById(id);
    }

    @Override
    public Teacher getTeacherByName(String teacherName) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_name", teacherName);
        return this.getOne(wrapper);
    }
}
